<?php

namespace app\mobile\controller;

use app\common\model\CardCzgive;
use app\common\model\CardCzLog;
use app\common\model\CardGoods;
use app\common\model\CardMemberAli;
use app\common\model\CardOrder;
use app\common\model\CardRank;
use app\common\model\CardUserGetAli;
use app\common\model\CardUserUpdateAli;
use app\common\model\Merchant;
use Alipay\Alipayobj;
use app\common\model\Agent;
use app\common\model\MerchantContent;
use app\common\model\Paylog;
use think\Controller;
use think\Session;
use Wechat\Wxobjopen;

class Carduserali extends Mobileali
{

    /*
     * 首页
     * $card_id
     *
     */
    public function index()
    {
        $this->_init();
        $arr = request()->param();
//        var_dump($arr);
//        $map['template_id'] = '20180310000000000852187000300944';
        $map['mid'] = $arr['mid'];
        $map['user_id'] = Session::get('card_user_id');
        $map['card_type'] = 'OUT_MEMBER_CARD';
        $map['have'] = 1;
        $CardUserGetAli = new CardUserGetAli();

        $info = $CardUserGetAli->where($map)->find();
        //不存在符合条件的则跳转到投放页面
//        if (empty($info)) {
//            $data['template_id'] = $arr['template_id'];
//            $data['openid'] = Session::get('card_openid');
//            $this->redirect('Carduserali/addcard', $data);
//        } else {
//            $info['balance'] = sprintf("%.2f", $info['balance']);
            $maps['template_id'] = $info['template_id'];
            $CardMemberAli = new CardMemberAli();
            $card = $CardMemberAli->where($maps)->find();
            $this->assign('card', $card);       //ka
            $this->assign('info', $info);       //user
//        var_dump(json_encode($info,JSON_UNESCAPED_UNICODE));
        return $this->fetch();
    }

    //在线充值
    /* 查询该会员卡的信息支付时用作传入参数
     *
     */
    public function recharge()
    {
        if (Request()->isAjax()) {
            if (Request()->post('price') > 0) {

            }else{
                $jsApiObj = ['code' => 9001, 'message' => '请输入正确的充值金额！'];
            }
        } else {
            $this->_init();
            $arr = request()->param();
            $CardUserGetAli = new CardUserGetAli();
            $CardCzgive = new CardCzgive();
//            $map['template_id'] = $arr['template_id'];
            $map['mid'] = $arr['mid'];
            $map['user_id'] = Session::get('card_user_id');
//            $map['user_id'] = '2088902885506096';
            $map['card_type'] = 'OUT_MEMBER_CARD';
            $info = $CardUserGetAli->where($map)->find();
//            var_dump(json_encode($info,JSON_UNESCAPED_UNICODE));
            $maps['template_id'] = $info['template_id'];
            $maps['statu'] = 1;
            $CardMemberAli = new CardMemberAli();
            $czgive = $CardCzgive->where($maps)->select();
            if ($info) {
                $user['mid'] = $CardMemberAli->where('template_id', $info['template_id'])->value('mid');
                $user['user_id'] = Session::get('card_user_id');
//                $user['user_id'] = '2088902885506096';
                $this->assign('user', $user);
                $this->assign('czgive', $czgive);
                $this->assign('info', $info);
                $is_zxcz = $CardMemberAli->where(['template_id'=> $info['template_id']])->value('is_zxcz');
                if ($is_zxcz == 1) {
                    return $this->fetch();
                } else {
                    return $this->fetch('off');
                }
            } else {
                $data['template_id'] = $info['template_id'];
                $data['user_id'] = Session::get('card_user_id');
                $this->redirect('Carduserali/addcard', $data);

            }
        }
    }

    public function alitest(){
//        $arr['price'] = '0.01';
//        $arr['mid'] = 501;
//        $arr['user_id'] = '2088902885506096';
//        $arr['orderid'] = 'H-2018031211072594868';
//        $arr['cardid'] = '3542';
//        $arr['giveid'] = '';
//        $res = $this->alipaypay($arr);
        $template_id = '20180309000000000846116000300943';
        $CardMemberAli = new CardMemberAli();
        $cardus = $CardMemberAli->where(['template_id'=>$template_id])->field('mid,top_mid,card_type')->find();
        return json($cardus);
    }

    public function alipaypay()
    {
        $request = request();
        $arr = $request->param();
//        file_put_contents('/data2/html/payos/test.txt', 'ali-1' . json_encode($arr), FILE_APPEND);
        if ($arr['price'] <= 0){
            return json(['code'=>1009,'msg'=>'金额异常']);
        }else {
            $Paylog = new Paylog();
            $orderid = $arr['orderid'];
            $norderid = $Paylog->where(array('orderid' => $orderid))->count('id');
            if ($norderid != 0) {
                echo 1;
            } else {
                $Merchant = new Merchant();
                $Agent = new Agent();
                $users = $Merchant->where(array('id' => $arr['mid']))->find();
                $agent = $Agent->where(array('id' => $users['agent']))->find();
                $options = array(
                    //'token' => $config ["token"], // 填写你设定的key
                    //'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
                    'appid' => $agent['ali_app_id'], // 填写高级调用功能的app id
                    'rsaPrivateKey' => $agent['ali_app_key'],

                );
                $aliObj = new Alipayobj($options);

                $price = $arr['price'];
                $buyer_id = $arr['user_id'];
                $out_trade_no = $orderid;
                $body = "快捷支付";
                $total_fee = $price;
                $data['app_id'] = $agent['ali_app_id'];
                $data['method'] = "alipay.trade.create";
                $data['charset'] = "UTF-8";
                $data['sign_type'] = "RSA2";
                $data['timestamp'] = date('Y-m-d H:i:s');
                $data['version'] = "1.0";
                $data['notify_url'] = 'https://' . $_SERVER ['SERVER_NAME'];
                $data['app_auth_token'] = $users['ali_merid'];
                $data['biz_content'] = json_encode(array(
                    'out_trade_no' => $out_trade_no,
                    'total_amount' => $total_fee,
                    'subject' => $body,
                    'buyer_id' => $buyer_id,
                    'alipay_store_id' => $users['ali_store_id'],
//                'store_id'=>I('get.id'),
                    'extend_params' => array(
                        'sys_service_provider_id' => $agent['sys_service_provider_id'],
                    ),

                ));
                $signContent = $aliObj->rsaSign($data, $signType = "RSA2");
                $data['sign'] = $signContent;
                $uid = $aliObj->getuid($data);
                $user = json_decode($uid, true);
                $trade_no = $user['alipay_trade_create_response']['trade_no'];
                $sdata['orderid'] = $out_trade_no;
                $sdata['mid'] = $arr['mid'];
                $sdata['openid'] = $arr['user_id'];
                $sdata['pay_statu'] = 0;
                $sdata['price'] = $price;
                $sdata['yf_price'] = $price;
//                $sdata['zk_price'] = $data['yf_price'] - $data['price'];
                $sdata['attach'] = 'cardcz';
                $sdata['cz_card_id'] = $arr['cardid'];
                $sdata['add_time'] = time();
                $sdata['pay_fs'] = 1;
                $Paylog = new Paylog();
                $Paylog->save($sdata);
                $CardCzLog = new CardCzLog();
                $CardCzgive = new CardCzgive();
                $czdata['user_id'] = $arr['user_id'];
                $czdata['cz_price'] = $price;
                $czdata['zs_price'] = $CardCzgive->where(['id' => $arr['giveid']])->value('zs_money');
                $czdata['orderid'] = $out_trade_no;
                $czdata['giveid'] = $arr['giveid'];
//                $czdata['cardgetid'] = $arr['cardid'];
                $czdata['addtime'] = time();
                $CardCzLog->save($czdata);
                return json($trade_no);
            }
        }


    }

    function payover()
    {
        $request = request();
        $Merchant = new Merchant();
        $Agent = new Agent();
        $users = $Merchant->where(array('id' => $request->param('id')))->find();
        $agent = $Agent->where(array('id' => $users['agent']))->find();
        $raskey = $agent['ali_app_key'];
        $options = array(
            //'token' => $config ["token"], // 填写你设定的key
            //'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
            'appid' => $agent['ali_app_id'], // 填写高级调用功能的app id
            'rsaPrivateKey' => $agent['ali_app_key'],

        );
        $aliObj = new Alipayobj($options);
        $appid = $agent['ali_app_id'];
        $data['app_id'] = $appid;
        $data['method'] = 'alipay.trade.query';
        $data['charset'] = "UTF-8";
        $data['sign_type'] = "RSA2";
        $data['timestamp'] = date('Y-m-d H:i:s');
        $data['version'] = "1.0";
        $data['app_auth_token'] = $users['ali_merid'];
        $data['biz_content'] = json_encode(array(
            'out_trade_no' => $request->param('orderid')
        ));
        $signContent = $aliObj->rsaSign($data, $signType = "RSA2");
        $data['sign'] = $signContent;
        $uid = $aliObj->getuid($data);
        $user = json_decode(iconv("GB2312", "UTF-8//IGNORE", $uid), true);
        $orderstatu = $user['alipay_trade_query_response']['trade_status'];
        if ($orderstatu == "TRADE_SUCCESS") {
            $data = 0;
        } else {
            $data = 1;
        }
        return json($data);
    }

    //订单
    public function getorder2()
    {
        $request = request();
        if ($request->isAjax()) {
            $sh = $request->post('mid');
            $order = "H" . $sh . "-" . date('YmdHis') . rand(10000, 99999);
            echo $order;
        }
    }


    //余额    查询该会员卡余额
    /*
     * $card_id
     *
     */
    public function balance($template_id='')
    {
        $this->_init();
        $arr = request()->param();
        $map['user_id'] = Session::get('card_user_id');
        if (empty($template_id)){
            $map['template_id'] = $arr['template_id'];
        }else{
            $map['template_id'] = $template_id;
        }
        $map['card_type'] = 'OUT_MEMBER_CARD';
        $map['have'] = 1;
        $CardUserGet = new CardUserGetAli();
        $info = $CardUserGet->where($map)->find();
        if ($info) {
            $arr = request()->param();
            $mab['user_id'] = $map['user_id'];
            $mab['template_id'] = $info['template_id'];
            $mab['add_balance|del_balance']= ['neq', 0];
            $CardUserUpdate = new CardUserUpdateAli();
            $CardMember = new CardMemberAli();
            $CardUserGet = new CardUserGetAli();
            $list = $CardUserUpdate->where($mab)->order('id desc')->select();
            $maps['template_id'] = $arr['template_id'];
            $maps['user_id'] = Session::get('card_user_id');
            $maps['have'] = 1;
            $card['balance'] = $CardUserGet->where($map)->value('balance');
            $card['brandname'] = $CardMember->where('template_id', $arr['template_id'])->value('brand_name');
            //JSSDK

            $is_money = $CardMember->where('template_id', $arr['template_id'])->value('is_money');
            if ($is_money == 1) {
                return $this->fetch();
            } else {
                return $this->fetch('off');
            }
        } else {
            $data['template_id'] = $arr['template_id'];
            $data['user_id'] = Session::get('card_user_id');
            $this->redirect('Carduserali/addcard', $data);
        }
    }

    //余额详情
    public function balancedetail(){
        $arr = request()->param();
        $map['id'] = $arr['dalanceid'];
        $map['template_id'] = $arr['template_id'];
        $map['add_balance|del_balance']= ['neq', 0];
        $CardUserUpdate = new CardUserUpdateAli();
        $info = $CardUserUpdate->where($map)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    //积分    查询该会员卡积分
    /*
     * $card_id
     */
    public function bonus($template_id = '')
    {
        $this->_init();
        $arr = request()->param();
        $map['user_id'] = Session::get('card_user_id');
        if (empty($template_id)){
            $map['template_id'] = $arr['template_id'];
        }else{
            $map['template_id'] = $template_id;
        }
        $map['card_type'] = 'OUT_MEMBER_CARD';
        $map['have'] = 1;
        $map['statu'] = 0;
        $CardUserGet = new CardUserGetAli();
        $info = $CardUserGet->where($map)->find();
        if ($info) {
            $arr = request()->param();
            $mab['template_id'] = $arr['template_id'];
            $mab['user_id'] = Session::get('card_user_id');
            $mab['add_bonus|del_bonus'] = array('neq', 0);
            $CardUserUpdate = new CardUserUpdateAli();
            $CardUserGet = new CardUserGetAli();
            $CardMember = new CardMemberAli();
            $list = $CardUserUpdate->where($mab)->order('id desc')->select();
            $maps['template_id'] = $arr['template_id'];
            $maps['user_id'] = Session::get('card_user_id');
            $card['bonus'] = $CardUserGet->where($maps)->value('bonus');
            $card['brandname'] = $CardMember->where('template_id', $arr['template_id'])->value('brand_name');

            $is_bonus = $CardMember->where('template_id', $arr['template_id'])->value('is_bonus');
            if ($is_bonus == 1) {
                return $this->fetch();
            } else {
                return $this->fetch('off');
            }
        } else {
            $this->redirect('Carduserali/weboff', ['template_id' => $template_id]);
        }
    }

    //余额
    //余额    查询该会员卡余额
    /*
     * $card_id
     *
     */
    public function balancelist($template_id='')
    {
        $this->_init();
        $arr = request()->param();
        $map['user_id'] = Session::get('card_user_id');
        if (empty($template_id)){
            $map['template_id'] = $arr['template_id'];
        }else{
            $map['template_id'] = $template_id;
        }
        $map['card_type'] = 'OUT_MEMBER_CARD';
        $map['have'] = 1;
        $CardUserGet = new CardUserGetAli();
        $info = $CardUserGet->where($map)->find();
        if ($info) {
            $arr = request()->param();
            $mab['user_id'] = $map['user_id'];
            $mab['template_id'] = $info['template_id'];
            if ($arr['type'] == "recharge"){
                $mab['add_balance']= ['neq', 0];
            }elseif($arr['type'] == "consum"){
                $mab['del_balance']= ['neq', 0];
            }
            $CardUserUpdate = new CardUserUpdateAli();
            $CardMember = new CardMemberAli();
            $CardUserGet = new CardUserGetAli();
            $list = $CardUserUpdate->where($mab)->order('id desc')->select();
            $maps['template_id'] = $arr['template_id'];
            $maps['user_id'] = Session::get('card_user_id');
            $maps['have'] = 1;
            $card['balance'] = $CardUserGet->where($map)->value('balance');
            $card['brandname'] = $CardMember->where('template_id', $arr['template_id'])->value('brand_name');


            $is_money = $CardMember->where('template_id', $arr['template_id'])->value('is_money');


            if ($is_money == 1) {
                return $this->fetch();
            } else {
                return $this->fetch('off');
            }
        } else {
            $data['template_id'] = $arr['template_id'];
            $data['user_id'] = Session::get('card_user_id');
            $this->redirect('Carduserali/addcard', $data);
        }
    }

    //等级
    /*
     * $card_id
     */
    public function level($template_id ='')
    {
        $this->_init();
        $arr = request()->param();
        $map['user_id'] = Session::get('card_user_id');
        if (empty($template_id)){
            $map['template_id'] = $arr['template_id'];
        }else{
            $map['template_id'] = $template_id;
        }
        $map['card_type'] = 'OUT_MEMBER_CARD';
        $map['have'] = 1;
        $CardUserGet = new CardUserGetAli();
        $info = $CardUserGet->where($map)->find();
        if ($info) {
            $arr = request()->param();
            if (empty($arr['code'])) {
                $map['user_id'] = Session::get('card_user_id');
            }
            $map['template_id'] = $arr['template_id'];

            $CardRank = new CardRank();
            $info = $CardUserGet->where($map)->find();
            //获取对应等级
            $maps['template_id'] = array('eq', $arr['template_id']);
            $level = $CardRank->where($maps)->select();
            //JSSDK

            $CardMember = new CardMemberAli();
            $is_rank = $CardMember->where('template_id', $template_id)->value('is_rank');
            if ($is_rank == 1) {
                return $this->fetch();
            } else {
                return $this->fetch('off');
            }
        } else {
            $this->redirect('Carduserali/weboff', ['template_id' => $template_id]);
        }
    }

    //等级奖励领取
    /*
     * ajax 方式     gid  即该会员在数据表中的id
     * */
    public function getRankReward(){
        if (Request()->isAjax()){
            $arr = request()->post();      //获取参数
            $CardRank = new CardRank();
            $CardUserGet = new CardUserGetAli();
            $map['id'] = $arr['gid'];
            $user = $CardUserGet->where($map)->field('bonus,user_id,code,template_id,rank_lock,balance')->find();  //会员信息
            $isarr['lid'] = $arr['lid'];
            $isarr['user_bonus'] = $user['bonus'];
//            return $isarr;
            $is_rank = $this->is_rank($isarr);      //等级判断
            if ($is_rank == 0){
                $rank = $CardRank->where(['id'=>$arr['lid']])->find();     //等级奖励
//                $level = getlive($user['template_id'],$user['bonus']);  //是否已领取
                if ($user['rank_lock'] < $rank['live']){
                    if ($rank['reward_bonus'] > 0 || $rank['reward_balance'] >0) {
                        $marke = new Cardmarke();
                        if ($rank['reward_bonus'] > 0){
                            $bonus['template_id'] = $user['template_id'];
                            $bonus['code'] = $user['code'];
                            $bonus['user_id'] = $user['user_id'];
                            $bonus['add_bonus'] = $rank['reward_bonus'];
                            $bonus['name'] = '等级奖励';
                            $bonus['outerstr'] = '等级奖励';
                            $resbonus = $marke->varybonus($bonus);
                        }else{
                            $resbonus = 0;
                        }
                        if ($rank['reward_balance'] > 0){
                            $balance['template_id'] = $user['template_id'];
                            $balance['code'] = $user['code'];
                            $balance['user_id'] = $user['user_id'];
                            $balance['add_balance'] = $rank['reward_balance'];
                            $balance['name'] = '等级奖励';
                            $balance['type'] = '3';
                            $balance['balance'] = $user['balance'] + $rank['reward_balance'];
                            $balance['outerstr'] = '等级奖励';
                            $resbalance = $marke->varybanlance($balance);
                        }else{
                            $resbalance = 0;
                        }
                        if ($resbalance == 0 && $resbonus == 0){
                            $ldata['rank_lock'] = $rank['live'];
                            $CardUserGet->save($ldata,$map);
                            $rarr['code'] = 0;
                            $rarr['message'] = '领取成功';
                        }else{
                            $rarr['code'] = 0;
                            $rarr['message'] = '领取失败';
                        }
                    }else{
                        $rarr['code'] = 9003;
                        $rarr['message'] = '无奖励';
                    }
                }else{
                    $rarr['code'] = 9002;
                    $rarr['message'] = '已领取';
                }
            }else{
                $rarr['code'] = 9001;
                $rarr['message'] = '未达到';

            }
            return json($rarr);
        }
    }

    //等级判断
    private function is_rank($arr){
        $CardRank = new CardRank();
        $map['id'] = $arr['lid'];
        $rank = $CardRank->where($map)->find();
        if ($rank['bonus'] <= $arr['user_bonus']){
            $data = 0;      //达到
        }else{
            $data = 1;      //未达到
        }
        return $data;
    }


    //我的卡券  查询所有属于该openid的会员卡
    /*
     * $card_id
     */
    public function mecard()
    {
//        $this->_init();
        $arr = request()->param();
        $CardMemberAli = new CardMemberAli();
        //我的会员卡
        $CardUserGetAli = new CardUserGetAli();
        //我的券
        $maps['user_id'] = Session::get('card_user_id');
        $maps['have'] = 1;
        $maps['card_type'] = array('eq', 'OUT_MEMBER_CARD');
        $maps['statu'] = ['neq', 2];
        $mevou = $CardUserGetAli->where($maps)->order('statu asc')->select();
        $this->assign('mevou', $mevou);
        $is_mecard = $CardMemberAli->where(['template_id'=>$arr['template_id']])->value('is_mecard');
        if ($is_mecard == 1) {
            return $this->fetch();
        } else {
            return $this->fetch('off');
        }
    }



    //适用门店
    /*
     * $card_id
     */
    public function storelist($template_id = '')
    {
        $this->_init();
        $arr = request()->param();
        $map['user_id'] = Session::get('card_user_id');
        if (empty($template_id)){
            $map['template_id'] = $arr['template_id'];
        }else{
            $map['template_id'] = $template_id;
        }
        $map['card_type'] = 'OUT_MEMBER_CARD';
        $map['have'] = 1;
        $CardUserGet = new CardUserGetAli();
        $info = $CardUserGet->where($map)->find();
//        return json($info);
        if ($info) {
            $CardMember = new CardMemberAli();
            $Merchant = new Merchant();
            $top_mid = $CardMember->where('template_id', $template_id)->value('top_mid');
//            $base_info = $CardMember->where('template_id', $template_id)->value('base_info');
//            $base_info = json_decode($base_info, true);
//            if ($base_info['use_all_locations'] == 'true') {
                $mab['top_mid'] = $top_mid;
//            } else {
//                $mapid = $base_info['location_id_list'];
//                $mab['mapid'] = ['in', $mapid];
//            }
            $storelist = $Merchant->where($mab)->select();
            $this->assign('list', $storelist);
            $is_locations = $CardMember->where('template_id', $template_id)->value('is_locations');
            if ($is_locations == 1) {
                return $this->fetch();
            } else {
                return $this->fetch('off');
            }
        }
    }

    //门店详情
    /*
     * 门店id
     * $id
     */
    public function storeinfo($id)
    {
        $Merchant = new Merchant();
        $MerchantContent = new MerchantContent();
        $map['id'] = $id;
        $info = $Merchant->where($map)->find();
        $info['phone'] = $MerchantContent->where('mid',$id)->value('tel');
        $this->assign('info', $info);
        return $this->fetch();
    }

    //会员卡详情
    /*
     * $card_id
     *
     */
    public function cardinfo($template_id ='')
    {
        $this->_init();
        $arr = request()->param();
        $map['user_id'] = Session::get('card_user_id');
        if (empty($template_id)){
            $map['template_id'] = $arr['template_id'];
        }else{
            $map['template_id'] = $template_id;
        }
        $map['card_type'] = 'OUT_MEMBER_CARD';
        $map['have'] = 1;
        $map['statu'] = 0;
        $CardUserGet = new CardUserGetAli();
        $info = $CardUserGet->where($map)->find();

        if ($info) {
            $CardMember = new CardMemberAli();
            $card = $CardMember->where('template_id', $template_id)->find();
//            $card['base_info'] = json_decode($card['base_info'], true);
//            $this->assign('card', $card);
            $is_card_info = $CardMember->where('template_id', $template_id)->value('is_card_info');
            if ($is_card_info == 1) {
                return $this->fetch();
            } else {
                return $this->fetch('off');
            }
        } else {
            $this->redirect('Carduserali/weboff', ['template_id' => $template_id]);
        }
    }

    //修改资料
    /*
     * $card_id
     *
     */
    public function userinfo($template_id = '')
    {
        $this->_init();
        $arr = request()->param();
        $map['user_id'] = Session::get('card_user_id');
        if (empty($template_id)){
            $map['template_id'] = $arr['template_id'];
        }else{
            $map['template_id'] = $template_id;
        }
        $map['card_type'] = 'OUT_MEMBER_CARD';
        $map['have'] = 1;
        $CardUserGet = new CardUserGetAli();
        $info = $CardUserGet->where($map)->find();
        if ($info) {
            //END
            $this->assign('info', $info);
            $CardMember = new CardMemberAli();
            $is_edit_user = $CardMember->where('template_id', $template_id)->value('is_edit_user');
            if ($is_edit_user == 1) {
                return $this->fetch();
            } else {
                return $this->fetch('off');
            }
        } else {
            $this->redirect('Carduserali/weboff', ['template_id' => $template_id]);
        }
    }

    //修改会员资料
    /*
     * $card_id
     *
     */
    public function editinfo()
    {
        $this->_init();
        $arr = request()->param();
        $map['user_id'] = Session::get('card_user_id');
        if (empty($template_id)){
            $map['template_id'] = $arr['template_id'];
        }else{
            $map['template_id'] = $template_id;
        }
        $map['card_type'] = 'OUT_MEMBER_CARD';
        $map['have'] = 1;
        $CardUserGet = new CardUserGetAli();
        $info = $CardUserGet->where($map)->find();
        if ($info) {
            $map['template_id'] = $arr['template_id'];

            //END
            $this->assign('info', $info);
            return $this->fetch();
        } else {
            $this->redirect('Carduserali/weboff', ['template_id' => $arr['template_id']]);
        }
    }

    //修改处理
    /*
     * $data['id'] $data['card_id']
     * POST 修改数据
     *
     */
    public function editinfohand(){
        if (Request()->isPost()){
            $data = request()->post();
            $map['id'] = $data['id'];
            $CardUserGet = new CardUserGetAli();
            $res = $CardUserGet->allowField(true)->save($data,$map);
            if ($res){
                $this->success('修改成功！',url('carduserali/editinfo', ['template_id' => $data['template_id']]));
//                $rarr['code'] = 0;
//                $rarr['message'] = '修改成功！';
            }else{
                $this->error('修改失败！',url('carduserali/editinfo', ['template_id' => $data['template_id']]));
//                $rarr['code'] = 9001;
//                $rarr['message'] = '修改失败';
            }
        }
    }

    //我的订单
    /*
     * $card_id
     *
     */
    public function orderme($template_id)
    {
        $this->_init();
        $map['template_id'] = $template_id;
        $map['user_id'] = Session::get('card_user_id');
        $map['have'] = 1;
        $map['card_type'] = 'OUT_MEMBER_CARD';
        $CardUserGet = new CardUserGetAli();
        $info = $CardUserGet->where($map)->find();
        if ($info) {
            $mab['user_id'] = Session::get('card_user_id');
            $mab['template_id'] = $template_id;
            $CardOrder = new CardOrder();
            $list = $CardOrder->where($mab)->order('id desc')->select();
            $card['template_id'] = $template_id;
            $this->assign('list', $list);
            $this->assign('card',$card);
            return $this->fetch();
        } else {
            $this->redirect('Carduserali/weboff', ['template_id' => $template_id]);
        }
    }

    //订单详情
    /*
     * 订单id 会员卡$card_id
     *
     * $id $card_id
     */
    public function orderinfo($id,$template_id){
        $this->_init();
        $map['template_id'] = $template_id;
        $map['user_id'] = Session::get('card_user_id');
        $map['have'] = 1;
        $map['card_type'] = 'OUT_MEMBER_CARD';
        $CardUserGet = new CardUserGetAli();
        $info = $CardUserGet->where($map)->find();
        if ($info) {
            $mab['user_id'] = Session::get('card_user_id');
            $mab['template_id'] = $template_id;
            $mab['id'] = $id;
            $CardGoods = new CardGoods();
            $CardOrder = new CardOrder();
            $order = $CardOrder->where($mab)->find();
            $goods = $CardGoods->where('id',$order['gid'])->find();
            $this->assign('goods',$goods);
            $this->assign('order', $order);
            return $this->fetch();
        } else {
            $this->redirect('Carduserali/weboff', ['template_id' => $template_id]);
        }
    }

    //功能开关   暂时弃用
    /*
     *  $arr['openid'] / $arr['card_id'] / $arr['is_dedu_cards']
     *
     */
    public function disetup(){
        if (Request()->isAjax()){
            $arr = request()->post();
            $CardUserGet = new CardUserGetAli();
            $map['openid'] = $arr['openid'];
            $map['template_id'] = $arr['template_id'];
            $map['have'] = 1;
            $map['card_type'] = 'MEMBER_CARD';
            $data['is_dedu_cards'] = $arr['is_dedu_cards'];
            $res = $CardUserGet->allowField('is_dedu_cards')->update($data,$map);
            if ($res){
                $rass['code'] = 0;
                $rass['message'] = '修改成功';
            }else{
                $rass['code'] = 9001;
                $rass['message'] = '修改失败';
            }
            return json($rass);
        }else{
            $this->_init();
            $arr = request()->param();
            $map['openid'] = Session::get('card_openid');
            $map['template_id'] = $arr['template_id'];
            $map['have'] = 1;
            $map['card_type'] = 'OUT_MEMBER_CARD';
            $CardUserGet = new CardUserGetAli();
            $info = $CardUserGet->where($map)->find();
            if ($info) {                                                                //会员信息
                $card['template_id'] = $arr['template_id'];
                $this->assign('info', $info);
                $this->assign('card',$card);
                return $this->fetch();
            } else {
                $this->redirect('Carduser/weboff', ['card_id' => $arr['card_id']]);        //关闭
            }
        }
    }

    //关闭页面 (jssdk 实现)
    /*
     * $card_id
     *
     */
    public function weboff($template_id)
    {
        //jssdk

        return $this->fetch('off');
    }


    //发放卡
    /*
     * $arr['openid'] / $arr['card_id']
     *
     *
     */
    public function addcard()
    {
//        $this->_init();
        $arr = request()->param();
        if (isset($arr['user_id']) && isset($arr['template_id'])) {

        }
    }

    //获取该门店所属商户的所有下属门店
    /*
     * $mid
     */
    private function getmidlist($mid){
        $Merchant = new Merchant();
        $top_mid = $Merchant->where('id',$mid)->value('top_mid');
        $midlist = $Merchant->where(['top_mid' => $top_mid])->field('id')->select();
        $smid=[];
        foreach ($midlist as $k => $v) {
            $smid[] = $v['id'];
        }
        $mids = implode(',', $smid);
        return $mids;
    }

    /* 获取创建该卡券的门店id
     *
     * $card_id
     *
     */
    private function getmid($template_id)
    {
        $CardMember = new CardMemberAli();
        $mid = $CardMember->where(['template_id' => $template_id])->value('mid');
        return $mid;
    }



}
